package com.club.controller;

import cn.dev33.satoken.annotation.SaCheckLogin;
import com.club.common.R;
import com.club.entity.Club;
import com.club.service.ClubService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 社团管理控制器
 */
@RestController
@RequestMapping("/club")
@Tag(name = "社团管理", description = "社团相关接口")
public class ClubController {

    @Autowired
    private ClubService clubService;

    /**
     * 创建社团
     */
    @SaCheckLogin
    @PostMapping("/create")
    @Operation(summary = "创建社团", description = "创建新的社团")
    public R createClub(@RequestBody Club club) {
        boolean success = clubService.createClub(club);
        return success ? R.ok().message("创建成功") : R.error().message("创建失败");
    }

    /**
     * 更新社团信息
     */
    @SaCheckLogin
    @PutMapping("/update")
    @Operation(summary = "更新社团信息", description = "更新社团的基本信息")
    public R updateClub(@RequestBody Club club) {
        boolean success = clubService.updateClub(club);
        return success ? R.ok().message("更新成功") : R.error().message("更新失败");
    }

    /**
     * 删除社团
     */
    @SaCheckLogin
    @DeleteMapping("/delete/{id}")
    @Operation(summary = "删除社团", description = "根据ID删除社团")
    public R deleteClub(@PathVariable("id") Long clubId) {
        boolean success = clubService.deleteClub(clubId);
        return success ? R.ok().message("删除成功") : R.error().message("删除失败");
    }

    /**
     * 查询社团详情
     */
    @GetMapping("/detail/{id}")
    @Operation(summary = "查询社团详情", description = "根据ID查询社团详细信息")
    public R getClubDetail(@PathVariable("id") Long clubId) {
        Club club = clubService.getById(clubId);
        return club != null ? R.ok().data(club) : R.error().message("社团不存在");
    }

    /**
     * 根据负责人ID查询社团列表
     */
    @SaCheckLogin
    @GetMapping("/by-leader/{leaderId}")
    @Operation(summary = "根据负责人查询社团", description = "查询指定负责人创建的社团列表")
    public R getClubsByLeaderId(@PathVariable("leaderId") Long leaderId) {
        List<Club> clubs = clubService.getClubsByLeaderId(leaderId);
        return R.ok().data(clubs);
    }

    /**
     * 根据用户ID查询加入的社团列表
     */
    @SaCheckLogin
    @GetMapping("/by-user/{userId}")
    @Operation(summary = "根据用户查询社团", description = "查询用户加入的所有社团列表")
    public R getClubsByUserId(@PathVariable("userId") Long userId) {
        List<Club> clubs = clubService.getClubsByUserId(userId);
        return R.ok().data(clubs);
    }

    /**
     * 查询所有社团列表
     */
    @GetMapping("/list")
    @Operation(summary = "查询社团列表", description = "查询所有社团，可按状态筛选")
    public R getAllClubs(@RequestParam(value = "status", required = false) Integer status) {
        List<Club> clubs = clubService.getAllClubs(status);
        return R.ok().data(clubs);
    }
}